﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using WX.WMS.DataModel;
using WX.WMS.Common;
using WX.WMS.BLL;

public partial class Transfer_TransStockDetail : PageBase
{
    private string action
    {
        get
        {
            if (ViewState["action"] == null) return "";
            else return ViewState["action"].ToString();
        }
        set { ViewState["action"] = value; }
    }
    private string _TransferID
    {
        get
        {
            if (ViewState["TransferID"] == null) return "";
            else return ViewState["TransferID"].ToString();
        }
        set { ViewState["TransferID"] = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            action = Request.QueryString["action"];
            if (action == null || action == "" || action.ToString() == "add")
            {
                action = "add";
                lblTitle.Text = "添加调仓单";
                CreateTime.Text = DateTime.Now.ToShortDateString();
                this.SerialNo.Visible = false;

            }
            else if (action == "edit")
            {
                lblTitle.Text = "编辑调仓单";
                _TransferID = Request.QueryString["ID"];
                DetailDiv.Visible = true;
                MainDelete.Visible = true;
                MainFinish.Visible = true;
                this.MainSave.Visible = false;

                TransferBLL transferBLL = new TransferBLL();
                T_TRANSFER transfer = transferBLL.GetTransferByTransferID(_TransferID);
                SearchResult.DataSource = transfer.T_TRANSFER_DETAILs;
                SearchResult.DataBind();

                this.CreateTime.Text = transfer.CREATE_TIME.Value.ToShortDateString();
                this.SerialNo.Text = Convert.ToString(transfer.SERIAL_NO);
                this.SerialNo.Visible = true;
            }
            else
            {
                lblTitle.Text = "查看调仓单";
                _TransferID = Request.QueryString["ID"];
                DetailDiv.Visible = false;
                MainDelete.Visible = false;
                MainFinish.Visible = false;
                MainSave.Visible = false;
                this.SearchResult.Columns[8].Visible = false;

                TransferBLL transferBLL = new TransferBLL();
                T_TRANSFER transfer = transferBLL.GetTransferByTransferID(_TransferID);
                SearchResult.DataSource = transfer.T_TRANSFER_DETAILs;
                SearchResult.DataBind();

                this.CreateTime.Text = transfer.CREATE_TIME.Value.ToShortDateString();
                this.SerialNo.Text = Convert.ToString(transfer.SERIAL_NO);
                this.SerialNo.Visible = true;
            }
            BoundStockList();
            BoundAreaList();
            BoundFloorList();

        }
        CreateTime.ReadOnly = true;
        //TransStockDate.Attributes["readonly"] = "readonly";
    }


    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        T_STORE Store = new StoreBLL().GetStoreByID(StoreID.Text.Trim());
        if (Store != null)
        {
            ProductsName.Value = Store.T_PRODUCT.NAME;
            Model.Value = Store.T_PRODUCT_DETAIL.MODEL;
            Standard.Value = Store.T_PRODUCT_DETAIL.STANDARD;
            Origin.Value = Store.T_PRODUCT_DETAIL.ORIGIN;
            Brand.Value = Store.T_PRODUCT_DETAIL.BRAND;
           // PalletQty.Value = Convert.ToString(Store.T_PRODUCT_DETAIL.PALLET_QTY);
            Size.Value = Store.T_PRODUCT_DETAIL.SIZE;
            LotNumber.Value = Store.T_PRODUCT_DETAIL.LOT_NUMBER;
            SubLotNumber.Value = Store.T_PRODUCT_DETAIL.SUB_LOT_NUMBER;
            PacketNO.Value = Store.T_PRODUCT_DETAIL.PACKET_NO;
            if (Store.T_PRODUCT_DETAIL.PRODUCTED_DATE == null)
                ProductedDate.Value = "";
            else
                ProductedDate.Value = Convert.ToString(Store.T_PRODUCT_DETAIL.PRODUCTED_DATE.Value.ToShortDateString());
            Stock.Value = Store.T_FLOOR.T_STOCK_AREA.T_STOCK.STOCK_NAME;
            Area.Value = Store.T_FLOOR.T_STOCK_AREA.AREA_NAME;
            Floor.Value = Store.T_FLOOR.FloorName;
            FloorID.Value = Convert.ToString(Store.FloorID);
            BarCode.Value = Store.T_PRODUCT_DETAIL.BAR_CODE;
            ProductDetailID.Value = Store.PRODUCT_DETAIL_ID.ToString();
            this.StockType.Value = Enum.GetName(typeof(Enumerations.StockType), Store.T_PRODUCT_DETAIL.STOCK_TYPE);
            this.KeyStandard.Value = Store.T_PRODUCT_DETAIL.KeyStandard;
            this.SupplyLotNumber.Value = Store.T_PRODUCT_DETAIL.SupplyLotNumber;
            this.BigClass.Value = Store.T_PRODUCT_DETAIL.BigClass;
            //   OtherStoreDate.Value = Convert.ToString(Store.T_PRODUCT_DETAIL.OTHER_STOCK_DATE.Value.ToShortDateString());

        }
    }
    protected void MainSave_Click(object sender, EventArgs e)
    {
        TransferBLL transferBLL = new TransferBLL();

        if (action == "add" && string.IsNullOrEmpty(_TransferID))
        {
            T_TRANSFER transfer = new T_TRANSFER();
            string NewTransferID = string.Empty;

            transfer.CREATE_TIME = DateTime.Now;
            transfer.CREATOR_ID = this.UserInfo.ID;
            transfer.STATE = (int)Enumerations.StockStatus.提交审核中;

            transferBLL.AddTransfer(transfer, out NewTransferID);

            _TransferID = NewTransferID;
            DetailDiv.Visible = true;
            MainDelete.Visible = true;
            MainFinish.Visible = true;
        }
        else
        {
            T_TRANSFER transfer = transferBLL.GetTransferByTransferID(_TransferID);

            transfer.CREATE_TIME = DateTime.Now;
            transfer.CREATOR_ID = this.UserInfo.ID;
            transfer.STATE = (int)Enumerations.StockStatus.提交审核中;

            transferBLL.UpdateTransfer(transfer);
        }

    }
    protected void MainDelete_Click(object sender, EventArgs e)
    {
        new TransferBLL().DeleteTransfer(_TransferID);
        //CreateTime.Text = string.Empty;
        SerialNo.Text = string.Empty;
        DetailDiv.Visible = false;
        MainDelete.Visible = false;
        MainFinish.Visible = false;
        SearchResult.DataSource = null;
        SearchResult.DataBind();
        _TransferID = "";
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        TransferDetailBLL transferDetailBLL = new TransferDetailBLL();
        T_TRANSFER_DETAIL Item = new T_TRANSFER_DETAIL();
        if (FloorID.Value.Trim() != string.Empty)
        {
            Item.FROM_FLOOR_ID = new Guid(FloorID.Value);
        }
        else
        {
            ShowMessage("移出的层不能为空！");
            return;
        }
        if (FloorTo.Items.Count > 0 && FloorTo.SelectedItem != null)
        {
            Item.TO_FLOOR_ID = new Guid(FloorTo.SelectedValue);
        }
        else
        {
            ShowMessage("移进的层不能为空！");
            return;
        }
        if (GrossWeight.Value.Trim() != string.Empty)
        {
            double pq;
            if (!double.TryParse(GrossWeight.Value.Trim(), out pq))
            {
                ShowMessage("毛重必须为数值类型");
                return;
            }
            Item.GROSS_WEIGHT = pq;
        }
        else
        {
            ShowMessage("毛重不能为空");
            return;
        }
        if (NetWeight.Value.Trim() != string.Empty)
        {
            double pq;
            if (!double.TryParse(NetWeight.Value.Trim(), out pq))
            {
                ShowMessage("净重必须为数值类型");
                return;
            }
            Item.NET_WEIGHT = pq;
        }
        else
        {
            ShowMessage("净重不能为空");
            return;
        }
        Item.TRANSFER_ID = new Guid(_TransferID);
        if (PacketQty.Value.Trim() != string.Empty)
        {
            double pq;
            if (!double.TryParse(PacketQty.Value.Trim(), out pq))
            {
                ShowMessage("大件数必须为数值类型");
                return;
            }
            Item.PACKET_QTY = pq;
        }
        else
        {
            ShowMessage("大件数不能为空");
            return;
        }
        if (PacketQty2.Value.Trim() != string.Empty)
        {
            double pq;
            if (!double.TryParse(PacketQty2.Value.Trim(), out pq))
            {
                ShowMessage("小件数必须为数值类型");
                return;
            }
            Item.PACKET_QTY2 = pq;
        }
        else
        {
            ShowMessage("小件数不能为空");
            return;
        }
        BaseDAO DAO = new BaseDAO();
        T_STORE storeFrom = DAO.T_STOREs.SingleOrDefault(t => t.PRODUCT_DETAIL_ID.ToString() == ProductDetailID.Value
                                                        && t.FloorID.ToString() == FloorID.Value);
       // storeFrom.GROSS_WEIGHT -= Convert.ToDouble(GrossWeight.Value.Trim());
        //storeFrom.NET_WEIGHT -= Convert.ToDouble(NetWeight.Value.Trim());
        //storeFrom.PACKET_QTY -= Convert.ToDouble(PacketQty.Value.Trim());
        //storeFrom.PACKET_QTY2 -= Convert.ToDouble(PacketQty2.Value.Trim());
        if (storeFrom.GROSS_WEIGHT - Convert.ToDouble(GrossWeight.Value.Trim()) < 0)
        {
            ShowMessage("输入毛重超过现有的最大数"+storeFrom.GROSS_WEIGHT);
            return;
        }
        if (storeFrom.NET_WEIGHT -Convert.ToDouble(NetWeight.Value.Trim())< 0)
        {
            ShowMessage("输入的净重超过现有的最大数"+storeFrom.NET_WEIGHT);
            return;
        }
        if (storeFrom.PACKET_QTY-Convert.ToDouble(PacketQty.Value.Trim()) < 0)
        {
            ShowMessage("输入的大件数超过现有的最大数"+storeFrom.PACKET_QTY);
            return;
        }
        if (storeFrom.PACKET_QTY2 - Convert.ToDouble(PacketQty2.Value.Trim()) < 0)
        {
            ShowMessage("输入的小件数超过现有的最大数"+storeFrom.PACKET_QTY2);
            return;
        }


        Item.REMARK = Remark.Value;
        Item.PRODUCT_DETAIL_ID = new Guid(ProductDetailID.Value);
        transferDetailBLL.AddTransferDetail(Item);
        BoundDetails(this._TransferID);
    }
    protected void MainFinish_Click(object sender, EventArgs e)
    {
        if (action == null || action == "" || action.ToString() == "add")
        {
            Response.Redirect("TransStockDetail.aspx?action=add");
        }
        else
        {
            Response.Redirect("TransStockManager.aspx");
        }
    }
    protected void SearchResult_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string ID = SearchResult.DataKeys[e.RowIndex]["ID"].ToString();
        TransferDetailBLL transferDetailBLL = new TransferDetailBLL();
        transferDetailBLL.DeleteTransferDetail(ID);
        BoundDetails(this._TransferID);
    }
    //protected void MainFinish0_Click(object sender, EventArgs e)
    //{
    //    Response.Redirect("TransStockManager.aspx");
    //}
    protected void SearchResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        SearchResult.PageIndex = e.NewPageIndex;
        BoundDetails(this._TransferID);
    }

    protected void BoundDetails(string ID)
    {
        TransferBLL transferBLL = new TransferBLL();
        SearchResult.DataSource = transferBLL.GetTransferByTransferID(ID).T_TRANSFER_DETAILs;
        SearchResult.DataBind();
    }

    protected void BoundStockList()
    {
        StockTo.DataSource = new StoreHouseBLL().GetStockList();
        StockTo.DataBind();
    }

    protected void BoundAreaList()
    {
        var source = new StoreHouseBLL().GetStockAreaList();
        AreaTo.DataSource = source.Where(t => t.STOCK_ID.ToString() == StockTo.SelectedValue);
        AreaTo.DataBind();
    }

    protected void BoundFloorList()
    {
        var source = new FloorBLL().GetFloorList();
        FloorTo.DataSource = source.Where(t => t.T_STOCK_AREA.AREA_ID.ToString() == AreaTo.SelectedValue);
        FloorTo.DataBind();
    }
    protected void StockTo_SelectedIndexChanged(object sender, EventArgs e)
    {
        BoundAreaList();
        BoundFloorList();
    }
    protected void AreaTo_SelectedIndexChanged(object sender, EventArgs e)
    {
        BoundFloorList();
    }
}
